﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Resources;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Net;


namespace NewsReader
{
    public class NewsFeed : INotifyPropertyChanged
    {
        private static RssProvider _currentProvider;
        private static AppSettings _settings;
        public NewsFeed()
        {
            this.TopStories = new ObservableCollection<RssItem>();
            SelectedFeed = new ObservableCollection<RssItem>();
            //_loadData();
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<RssItem> TopStories { get; private set; }

        public ObservableCollection<RssItem> SelectedFeed { get; private set; }

        //public AppSettings Settings
        //{
        //    get
        //    {
        //        if (_settings == null)
        //        {
        //            _settings = new AppSettings();
        //            NotifyPropertyChanged("Settings");
        //        }
        //        return _settings;
        //    }
        //}
        

        public RssProvider CurrentProvider
        {

            get
            {
                //RssProvider _currentProvider;
                if (_currentProvider == null)
                {
                    var providers = DataProvider.GetRssProviders();
                    _currentProvider = providers.FirstOrDefault(p => p.Code == App.Settings.ProviderSetting) ?? providers.First();
                    NotifyPropertyChanged("CurrentProvider");

                }
                return _currentProvider;
            }
            set
            {
                if (value != null)
                {
                    _currentProvider = value;
                    NotifyPropertyChanged("CurrentProvider");
                }
            }
        }
      
        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void Refresh()
        {
            this.TopStories.Clear();

            _loadData();
        }

        private void _loadData()
        {
            App.CurrentProvider.GetRssFeedCompleted += new EventHandler<GetRssFeedCompletedEventArgs>(CurrentProvider_GetRssFeedCompleted);
            App.CurrentProvider.GetRssFeedAsync(RssFeedTopics.TopStories);
        }

        void CurrentProvider_GetRssFeedCompleted(object sender, GetRssFeedCompletedEventArgs e)
        {

            App.CurrentProvider.GetRssFeedCompleted -= new EventHandler<GetRssFeedCompletedEventArgs>(CurrentProvider_GetRssFeedCompleted);
            foreach (var item in e.Result)
            {
                this.TopStories.Add(item);
            }
            App.IsDataLoaded = true;
        }
        public void LoadFeed(string topic)
        {
            this.SelectedFeed.Clear();
            App.CurrentProvider.GetRssFeedCompleted += new EventHandler<GetRssFeedCompletedEventArgs>(CurrentProvider_GetSelectedRssFeedCompleted);
            App.CurrentProvider.GetRssFeedAsync(topic);

        }
        void CurrentProvider_GetSelectedRssFeedCompleted(object sender, GetRssFeedCompletedEventArgs e)
        {
            App.CurrentProvider.GetRssFeedCompleted -= new EventHandler<GetRssFeedCompletedEventArgs>(CurrentProvider_GetSelectedRssFeedCompleted);
            foreach (var item in e.Result)
            {
                this.SelectedFeed.Add(item);
            }
            //App.IsDataLoaded = true;
        }
        public void ResetProvider()
        {
            _currentProvider = null;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}